package com.hoshiicloud.purchase.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.purchase.WorksheetProfile;
import com.hoshiicloud.purchase.feign.IdGeneratorService;
import com.hoshiicloud.purchase.mapper.WorksheetProfileMapper;
import com.hoshiicloud.purchase.service.WorksheetProfileService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统工单默认配置 服务实现类
 * TODO 每个平台的配置初始化
 * </p>
 *
 * @author Mr.wu
 * @since 2019-07-25
 */
@Service
public class WorksheetProfileServiceImpl extends ServiceImpl<WorksheetProfileMapper, WorksheetProfile> implements WorksheetProfileService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
    * @param id
    * @return com.hoshiibuy.purchase.entity.WorksheetProfile
    * @description: 根据id查询系统工单默认配置
    */
    @Cacheable(cacheNames = "WorksheetProfile", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "工单配置-系统工单默认配置-查询系统工单默认配置 ")
    public WorksheetProfile getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增系统工单默认配置
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "WorksheetProfile", allEntries = true)
    @Logable(authName = "工单配置-系统工单默认配置-新增系统工单默认配置")
    public boolean save(WorksheetProfile entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改系统工单默认配置
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "WorksheetProfile", allEntries = true)
    @Logable(authName = "工单配置-系统工单默认配置管理-修改系统工单默认配置")
    public boolean updateById(WorksheetProfile entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询系统工单默认配置
    */
    @Logable(authName = "工单配置-系统工单默认配置管理-分页查询系统工单默认配置")
    @Cacheable(cacheNames = "WorksheetProfile", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ',queryWrapper=' + #queryWrapper.toString() + ')')")
    public IPage<WorksheetProfile> page(IPage<WorksheetProfile> page, Wrapper<WorksheetProfile> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "WorksheetProfile", allEntries = true)
    @Logable(authName = "工单配置-系统工单默认配置管理-删除系统工单默认配置")
    public boolean deleteById(WorksheetProfile entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Cacheable(cacheNames = "WorksheetProfile", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "工单配置-系统工单默认配置管理-根据id集合查询系统工单默认配置")
    public List<WorksheetProfile> getByIds(List<Long> ids,String key) {
        return baseMapper.selectBatchIds(ids);
    }


    @CacheEvict(cacheNames = "WorksheetProfile", allEntries = true)
    @Override
    @Logable(authName = "工单配置-系统工单默认配置管理-初始化系统工单默认配置")
    public boolean initProfile(Long companyId, Long platformId, Long storeId) {

        /**
         * 根据Map中的编码和类型生成默认工单配置
         *  编码格式待定
         *  TODO 配置从数据库读取
         *  1.查找指定平台的默认处理人
         *  2.生成默认配置
         */
        //默认配置的编码和类型
        Map<String,Integer> profileMap=new HashMap<>();
        //处理人id
        Long userId=null;
        if(companyId!=null && platformId!=null && storeId!=null){
            //生成店铺工单配置
            profileMap.put("A0001",1);
        }
        else if(companyId!=null && platformId!=null){
            //生成平台工单配置
            profileMap.put("A0001",1);
        }
        else if(companyId!=null){
            //生成租户工单配置
            profileMap.put("A0001",1);
        }

        for(Map.Entry<String,Integer> entry:profileMap.entrySet()){

        }
        return false;
    }
}
